package modelo.dao.impl;

import java.sql.*;
import java.util.*;
import modelo.dao.inter.ILoginDAO;
import modelo.dto.*;

public class LoginImpl extends GenericoImpl implements ILoginDAO {

	public LoginImpl(Connection conn) {
		super(conn);
	}

	@Override
	public boolean cadastrar(Object obj) throws SQLException {
		Login login = (Login) obj;	
        
        super.sql = "INSERT INTO login(usuario, login, senha, email) VALUES(?,?,?,?)";             
        
        try {
            super.pstm = super.conn.prepareStatement(super.sql);
            super.pstm.setString(1, login.getUsuario());
            super.pstm.setString(2, login.getLogin());
            super.pstm.setString(3, login.getSenha());
            super.pstm.setString(4, login.getEmail().getEnderecoEmail());
            
            System.out.println(this.pstm.toString());
            
            super.efetuou = !super.pstm.execute();
            
            super.conn.commit();
                                   
        } 
        catch (SQLException e) {
        	super.conn.rollback();
        	e.printStackTrace();
            System.out.println("Erro: "+e.getMessage());
            throw new SQLException(e.getMessage());
        }
        
        return super.efetuou;
	}

	@Override
	public boolean atualizar(Object obj) throws SQLException {
		Login login = (Login) obj;
		
		super.sql = "UPDATE login SET usuario= ?, login= ?, senha=?, email= ? WHERE id_login= ?";
		
		try {
			super.pstm = super.conn.prepareStatement(super.sql);
			super.pstm.setString(1, login.getUsuario());
			super.pstm.setString(2, login.getLogin());
			super.pstm.setString(3, login.getSenha());
			super.pstm.setString(4, login.getEmail().getEnderecoEmail());
			super.pstm.setInt(5, login.getIdLogin());
			
			super.efetuou = !super.pstm.execute();
			
			super.conn.commit();
		}
        catch (SQLException e) {        	
        	super.pstm.getConnection().rollback();
            System.out.println("Erro: "+e.getMessage());
            throw new SQLException(e.getMessage());
        }			
		
		return super.efetuou;
	}

	@Override
	public boolean excluir(Object obj) throws SQLException {

		return super.efetuou;
	}

	@Override
	public List<Login> buscarTodos() throws SQLException {
		List<Login> logins = new ArrayList<Login>();
		
		return logins;
	}	

	@Override
	public Login getObjetoById(Object obj) throws SQLException {
		Login login = (Login) obj;
		Login log = null;
		
        super.sql = "SELECT * FROM login WHERE id_login = ?";
        
        try {                        
            super.pstm = super.conn.prepareStatement(super.sql);            
            super.pstm.setInt(1, login.getIdLogin());
            
            super.res = super.pstm.executeQuery();
            
            super.conn.commit();
            
            if(super.res.next()){            	            	           
                log = new Login();
                log.setIdLogin(super.res.getInt("id_login"));                                                                                           
                log.setUsuario(super.res.getString("usuario"));
                log.setLogin(super.res.getString("login"));
                log.setSenha(super.res.getString("senha"));
                log.getEmail().setEnderecoEmail(super.res.getString("email"));
            }
            
        } 
        catch (SQLException e) {        	
        	super.pstm.getConnection().rollback();
            System.out.println("Erro: "+e.getMessage());
            throw new SQLException(e.getMessage());
        }		
		
		return log;
	}
	
	@Override
	public Login efetuarLogin(Login login) throws SQLException {
		Login log = null;
		
        super.sql = "SELECT * FROM login WHERE login= ? OR senha= ?";
        
        try {                        
            super.pstm = super.conn.prepareStatement(super.sql);            
            super.pstm.setString(1, login.getLogin());
            super.pstm.setString(2, login.getSenha());
            
            super.res = super.pstm.executeQuery();
            
            super.conn.commit();
            
            if(super.res.next()){            	            	           
                log = new Login();
                log.setIdLogin(super.res.getInt("id_login"));                                                                                           
                log.setUsuario(super.res.getString("usuario"));
                log.setLogin(super.res.getString("login"));
                log.setSenha(super.res.getString("senha"));
                log.getEmail().setEnderecoEmail(super.res.getString("email"));
            }
            
        } 
        catch (SQLException e) {        	
        	super.conn.rollback();
            System.out.println("Erro: "+e.getMessage());
            throw new SQLException(e.getMessage());
        }		
		
		return log;
	}

	@Override
	public Login getLoginByEmail(Email email) throws SQLException {
		Login log = null;		
		
        super.sql = "SELECT * FROM login WHERE email= ?";
        
        try {
            super.pstm = super.conn.prepareStatement(super.sql);
            super.pstm.setString(1, email.getEnderecoEmail());
            
            super.res = super.pstm.executeQuery();            
            
            if(super.res.next()){            	            	           
                log = new Login();
                log.setIdLogin(super.res.getInt("id_login"));                                                                                           
                log.setUsuario(super.res.getString("usuario"));
                log.setLogin(super.res.getString("login"));
                log.setSenha(super.res.getString("senha"));
                log.getEmail().setEnderecoEmail(super.res.getString("email"));
            }
            
        }
        catch (SQLException e) {
            System.out.println("Erro: "+e.getMessage());
            throw new SQLException(e.getMessage());
        }
		
		return log;
	}

	@Override
	public boolean atualizarSenha(String senha, Integer id) throws SQLException {
		
		super.sql = "UPDATE login SET senha=? WHERE id_login= ?";
		
		try {
			super.pstm = super.conn.prepareStatement(super.sql);
			super.pstm.setString(1, senha);
			super.pstm.setInt(2, id);
			
			super.efetuou = !super.pstm.execute();			
		}
        catch (SQLException e) {
        	super.conn.rollback();
            System.out.println("Erro: "+e.getMessage());
            throw new SQLException(e.getMessage());
        }			
		
		return super.efetuou;
	}

}

